﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Routing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SportsStore.Data;
using SportsStore.Data.Implementation;
using SportsStore.Data.SeedData;
using SportsStore.Models;

namespace SportsStore
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public Startup(IConfiguration configuration) => Configuration = configuration;
        public IConfiguration Configuration { get; }
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<ApplicationDbContext>(
                options => options.UseSqlServer(Configuration["Data:SportsStore:ConnectionString"],action=>action.UseRowNumberForPaging()));
            services.AddDbContext<AppIdentityDbContext>(
                options => options.UseSqlServer(Configuration["Data:SportsStoreIdentity:ConnectionString"], action => action.UseRowNumberForPaging()));
            services.AddIdentity<IdentityUser, IdentityRole>()
                .AddEntityFrameworkStores<AppIdentityDbContext>()
                .AddDefaultTokenProviders();
            services.AddTransient<IProductRepository, EfProductRepository>();
            services.AddTransient<IOrderRepository, EfOrderRepository>();
            services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddScoped(sp => SessionCart.GetCart(sp));
            services.AddMvc();
            services.AddMemoryCache();
            services.AddSession();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseDeveloperExceptionPage();
            app.UseStatusCodePages();
            app.UseStaticFiles();
            app.UseSession();
            app.UseAuthentication();
            app.UseMvc(routes =>
            {
                MapRoute(routes);
            });

            ProductSeedData.EnsurePopulated(app);
            IdentitySeedData.EnsurePopulated(app);
        }

        private void MapRoute(IRouteBuilder routes)
        {
            routes.MapRoute(
                name: null,
                template: "{category}/Page{productPage:int}",
                defaults: new { controller = "Product", action = "Index" });

            routes.MapRoute(
                name: null,
                template: "Page{productPage:int}",
                defaults: new { controller = "Product", action = "Index" ,productPage=1});

            routes.MapRoute(
                name: null,
                template: "{category}",
                defaults: new { controller = "Product", action = "Index", productPage = 1 });

            routes.MapRoute(
                name: null,
                template: "",
                defaults: new { controller = "Product", action = "index", productPage = 1 });

            routes.MapRoute(
                   name: "default",
                   template: "{controller}/{action}/{id?}");
        }
    }
}
